Skip to contentMethod: appendNewNodes(ReferencedListValueDescriptor, int, Resource)
1: /**
2: * Copyright (C) 2020 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.jena.list;
16:
17: import cz.cvut.kbss.ontodriver.descriptor.ReferencedListDescriptor;
18: import cz.cvut.kbss.ontodriver.descriptor.ReferencedListValueDescriptor;
19: import cz.cvut.kbss.ontodriver.jena.connector.StorageConnector;
20: import cz.cvut.kbss.ontodriver.model.NamedResource;
21: import org.apache.jena.rdf.model.Property;
22: import org.apache.jena.rdf.model.Resource;
23: import org.apache.jena.rdf.model.Statement;
24:
25: import java.net.URI;
26: import java.util.ArrayList;
27: import java.util.Collection;
28: import java.util.List;
29:
30: import static org.apache.jena.rdf.model.ResourceFactory.*;
31:
32: class ReferencedListHandler extends ListHandler<ReferencedListDescriptor, ReferencedListValueDescriptor> {
33:
34: ReferencedListHandler(StorageConnector connector) {
35: super(connector);
36: }
37:
38: @Override
39: AbstractListIterator iterator(ReferencedListDescriptor descriptor) {
40: return new ReferencedListIterator(descriptor, connector);
41: }
42:
43: @Override
44: AbstractListIterator iterator(ReferencedListValueDescriptor descriptor) {
45: return iterator((ReferencedListDescriptor) descriptor);
46: }
47:
48: @Override
49: void appendNewNodes(ReferencedListValueDescriptor descriptor, int i, Resource lastNode) {
50:• assert lastNode != null;
51: final List<Statement> toAdd = new ArrayList<>((descriptor.getValues().size() - i) * 2);
52: final Property hasList = createProperty(descriptor.getListProperty().getIdentifier().toString());
53: final Property hasNext = createProperty(descriptor.getNextNode().getIdentifier().toString());
54: final Property hasContent = createProperty(descriptor.getNodeContent().getIdentifier().toString());
55:• final String context = descriptor.getContext() != null ? descriptor.getContext().toString() : null;
56:• for (; i < descriptor.getValues().size(); i++) {
57: lastNode =
58:• appendNode(lastNode, descriptor.getValues().get(i), i == 0 ? hasList : hasNext, hasContent, context,
59: toAdd);
60: }
61: connector.add(toAdd, context);
62: }
63:
64: private Resource appendNode(Resource previousNode, NamedResource value, Property link, Property hasContent,
65: String context, List<Statement> statements) {
66: final Resource node = generateNewListNode(value.getIdentifier(), context);
67: statements.add(createStatement(previousNode, link, node));
68: statements.add(createStatement(node, hasContent, createResource(value.getIdentifier().toString())));
69: return node;
70: }
71:
72: private Resource generateNewListNode(URI baseUri, String context) {
73: Resource node;
74: int index = 0;
75: Collection<Statement> statements;
76: do {
77: node = createResource(baseUri.toString() + "-SEQ_" + index++);
78: statements = connector.find(node, null, null, context);
79: } while (!statements.isEmpty());
80: return node;
81: }
82: }